#include "display.h"

LED_Display __xdata ledDisplay;

/* bit[7:1]为数字显示，bit[0]为相关led控制 */
const u8 displayNum[10] =  {0x77, 0x24, 0x5D, 0x6D, 0x2E, 0x6B, 0x7B, 0x25, 0x7F, 0x6F}; //0 ~ 9

const u8 displayAlpha[] = {0x3F, 0x7A, 0x53, 0x7C, 0x5B, 0x1B, 0x73, 0x3F, 0x24, 0x74, 0x5E, 0x52, 0x39, 0x38, 0x77};//A ~ G

const u8 displayNegative = 0x08;    // '-'

const u8 displayLuminanceTab[9] = 
{
    BACKLIGHT_LUMINANCE_OFF,
    BACKLIGHT_LUMINANCE_LEVEL1,
    BACKLIGHT_LUMINANCE_LEVEL1,
    BACKLIGHT_LUMINANCE_LEVEL2,
    BACKLIGHT_LUMINANCE_LEVEL3,
    BACKLIGHT_LUMINANCE_LEVEL4,
    BACKLIGHT_LUMINANCE_LEVEL5,
    BACKLIGHT_LUMINANCE_LEVEL6,
    BACKLIGHT_LUMINANCE_LEVEL7
};

void led_display(void)
{
    tm1640_set_output((u8 *)&ledDisplay, sizeof(LED_Display));
}

static void display_set_luminance(u8 luminance)
{
    tm1640_set_luminance(luminance);
}

static void gpio_config(void)
{
    GPIO_InitTypeDef __xdata GPIO_InitStructure;        //结构定义
    GPIO_InitStructure.Pin = GPIO_Pin_4;        //指定要初始化的IO,
    GPIO_InitStructure.Mode = GPIO_OUT_PP;      //指定IO的输入或输出方式
    GPIO_Inilize(GPIO_P2, &GPIO_InitStructure); //初始化
    GPIO_InitStructure.Pin = GPIO_Pin_5;        //指定要初始化的IO,
    GPIO_InitStructure.Mode = GPIO_OUT_PP;      //指定IO的输入或输出方式
    GPIO_Inilize(GPIO_P2, &GPIO_InitStructure); //初始化
    GPIO_InitStructure.Pin = GPIO_Pin_6;        //指定要初始化的IO,
    GPIO_InitStructure.Mode = GPIO_OUT_PP;      //指定IO的输入或输出方式
    GPIO_Inilize(GPIO_P2, &GPIO_InitStructure); //初始化
    ICON_BTN_POWER_OFF();
    ICON_BTN_PAGE_OFF();
    ICON_BTN_OTHER_OFF();
}

/* 例：LED12，则displayX = 1, n = 2 */
static void display_ledx(u8 displayX, u8 n, u8 isOn)
{
    u8 __xdata *pLedDisplay = (u8* __xdata)&ledDisplay;
    if(isOn)
    {
        pLedDisplay[displayX] |= (1 << (n - 1));
    }
    else
    {
        pLedDisplay[displayX] &= ~(1 << (n - 1));
    }
}

static void display_icon_show(u8 icon, u8 state)
{
    switch(icon)
    {
        case ICON_STATUS_WIFI:              //D64
            display_ledx(6, 4, state);
            break;
        case ICON_STATUS_LOCK:              //D65
            display_ledx(6, 5, state);
            break;
        case ICON_STATUS_ELECTRIC_VALVE:    //D66
            display_ledx(6, 6, state);
            break;
        case ICON_STATUS_FLOOR_HEATING:     //D67
            display_ledx(6, 7, state);
            break;
        case ICON_MODE_FRESH_AIR:           //D68
            display_ledx(6, 8, state);
            break;
        case ICON_MODE_ECONOMIC:            //D61
            display_ledx(6, 1, state);
            break;
        case ICON_MODE_FLOOR_HEATING:       //D72
            display_ledx(7, 2, state);
            break;
        case ICON_MODE_COOLING:             //D79
            display_ledx(8, 1, state);
            break;
        case ICON_MODE_HEATING:             //D62
            display_ledx(6, 2, state);
            break;
        case ICON_MODE_AIR_CIRCULATION:     //D63
            display_ledx(6, 3, state);
            break;
        case ICON_MODE_DEHUMIDIFICATION:    //D73
            display_ledx(7, 3, state);
            break;
        case ICON_WIND_SPEED_AUTO:          //D78
            display_ledx(7, 8, state);
            break;
        case ICON_WIND_SPEED_LOW:           //D77;D76
            ledDisplay.data7 &= ~0x79;
            display_ledx(7, 7, state);
            display_ledx(7, 6, state);
            break;
        case ICON_WIND_SPEED_MEDIUM:        //D74;D75;D76;D77;
            ledDisplay.data7 &= ~0x79;
            display_ledx(7, 7, state);
            display_ledx(7, 6, state);
            display_ledx(7, 5, state);
            display_ledx(7, 4, state);
            break;
        case ICON_WIND_SPEED_HIGH:          //D71;D74;D75;D76;D77;
            ledDisplay.data7 &= ~0x79;
            display_ledx(7, 7, state);
            display_ledx(7, 6, state);
            display_ledx(7, 5, state);
            display_ledx(7, 4, state);
            display_ledx(7, 1, state);
            break;
        case ICON_BTN_POWER:                
            if(state == ENABLE)
                ICON_BTN_POWER_ON();
            else
                ICON_BTN_POWER_OFF();
            break;
        case ICON_BTN_PAGE:                
            if(state == ENABLE)
                ICON_BTN_PAGE_ON();
            else
                ICON_BTN_PAGE_OFF();
            break;
        case ICON_BTN_OTHER:                 
            if(state == ENABLE)
                ICON_BTN_OTHER_ON();
            else
                ICON_BTN_OTHER_OFF();
            break;
        default:
            break;
    }
}

static void display_icon_clear(void)
{
    for(u8 __xdata i = 0; i < ICON_MAX_NUM; i++)
    {
        display_icon_show(i, DISABLE);
    }
}

void display_clear(void)
{
    ledDisplay.data0 = 0;
    ledDisplay.data1 = 0;
    ledDisplay.data2 = 0;
    ledDisplay.data3 = 0;
    ledDisplay.data4 = 0;
    ledDisplay.data5 = 0;
    ledDisplay.data6 = 0;
    ledDisplay.data7 = 0;
    ledDisplay.data8 = 0;
    ICON_BTN_POWER_OFF();
    ICON_BTN_PAGE_OFF();
    ICON_BTN_OTHER_OFF();
}

static void display_tubex_dec(u8 tubex, u8 value, u8 isPositive, u8 isOn)
{
    u8 __xdata temp = 0;
    switch(tubex)
    {
        case TUBE_ID_1:
        {
            ledDisplay.data0 = 0;
            ledDisplay.data1 = 0;
            if(isOn)
            {
                if(isPositive)
                {
                    while(value >= 10)
                    {
                        value -= 10;
                        temp++;
                    }
                    ledDisplay.data0 |= displayNum[temp];
                }
                else
                {
                    ledDisplay.data0 |= displayNegative;
                }
                ledDisplay.data1 |= displayNum[value % 10];
            }
        }
        break;

        case TUBE_ID_2:
        {
            ledDisplay.data2 = 0;
            ledDisplay.data3 = 0;
            if(isOn)
            {
                if(isPositive)
                {
                    while(value >= 10)
                    {
                        value -= 10;
                        temp++;
                    }
                    ledDisplay.data2 |= displayNum[temp];
                }
                else
                {
                    ledDisplay.data2 |= displayNegative;
                }
                ledDisplay.data3 |= displayNum[value % 10];
            }
        }
        break;

        case TUBE_ID_3:
        {
            ledDisplay.data4 = 0;
            ledDisplay.data5 = 0;
            if(isOn)
            {
                if(isPositive)
                {
                    while(value >= 10)
                    {
                        value -= 10;
                        temp++;
                    }
                    ledDisplay.data4 |= displayNum[temp];
                }
                else
                {
                    ledDisplay.data4 |= displayNegative;
                }
                ledDisplay.data5 |= displayNum[value % 10];
            }
        }
        break;

        default:
            break;
    }
}

static void display_tubex_hex(u8 tubex, u8 value, u8 isOn)
{
    u8 __xdata valueHex[2];

    valueHex[0] = value % 16;
    valueHex[1] = value / 16;

    if(valueHex[0] < 10)
        valueHex[0] = displayNum[valueHex[0]];
    else
        valueHex[0] = displayAlpha[valueHex[0] - 10];
    
    if(valueHex[1] < 10)
        valueHex[1] = displayNum[valueHex[1]];
    else
        valueHex[1] = displayAlpha[valueHex[1] - 10];

    switch(tubex)
    {
        case TUBE_ID_1:
        {
            ledDisplay.data0 = 0;
            ledDisplay.data1 = 0;
            if(isOn)
            {
                ledDisplay.data0 |= valueHex[1];
                ledDisplay.data1 |= valueHex[0];
            }
        }
        break;

        case TUBE_ID_2:
        {
            ledDisplay.data2 = 0;
            ledDisplay.data3 = 0;
            if(isOn)
            {
                ledDisplay.data2 |= valueHex[1];
                ledDisplay.data3 |= valueHex[0];
            }
        }
        break;

        case TUBE_ID_3:
        {
            ledDisplay.data4 = 0;
            ledDisplay.data5 = 0;
            if(isOn)
            {
                ledDisplay.data4 |= valueHex[1];
                ledDisplay.data5 |= valueHex[0];
            }
        }
        break;

        default:
            break;
    }
}

static void display_tubex_switch(u8 tubex, u8 value, u8 isOn)
{
    switch(tubex)
    {
        case TUBE_ID_1:
        {
            ledDisplay.data0 = 0;
            ledDisplay.data1 = 0;
            if(isOn)
            {
                ledDisplay.data0 |= displayAlpha['O' - 'A'];
                if(value)
                    ledDisplay.data1 |= displayAlpha['N' - 'A'];
                else
                    ledDisplay.data1 |= displayAlpha['F' - 'A'];
            }
        }
        break;

        case TUBE_ID_2:
        {
            ledDisplay.data2 = 0;
            ledDisplay.data3 = 0;
            if(isOn)
            {
                ledDisplay.data2 |= displayAlpha['O' - 'A'];
                if(value)
                    ledDisplay.data3 |= displayAlpha['N' - 'A'];
                else
                    ledDisplay.data3 |= displayAlpha['F' - 'A'];
            }
        }
        break;

        case TUBE_ID_3:
        {
            ledDisplay.data4 = 0;
            ledDisplay.data5 = 0;
            if(isOn)
            {
                ledDisplay.data4 |= displayAlpha['O' - 'A'];
                if(value)
                    ledDisplay.data5 |= displayAlpha['N' - 'A'];
                else
                    ledDisplay.data5 |= displayAlpha['F' - 'A'];
            }
        }
        break;

        default:
            break;
    }
}

static void display_tubex_unit(u8 tubex, u8 value, u8 isOn)
{
    switch(tubex)
    {
        case TUBE_ID_1:
        {
            ledDisplay.data0 = 0;
            ledDisplay.data1 = 0;
            if(isOn)
            {
                ledDisplay.data0 |= displayAlpha['O' - 'A'];
                if(value == TEMPERATURE_UNIT_CELSIUS)
                    ledDisplay.data1 |= displayAlpha['C' - 'A'];
                else
                    ledDisplay.data1 |= displayAlpha['F' - 'A'];
            }
        }
        break;

        case TUBE_ID_2:
        {
            ledDisplay.data2 = 0;
            ledDisplay.data3 = 0;
            if(isOn)
            {
                ledDisplay.data2 |= displayAlpha['O' - 'A'];
                if(value == TEMPERATURE_UNIT_CELSIUS)
                    ledDisplay.data3 |= displayAlpha['C' - 'A'];
                else
                    ledDisplay.data3 |= displayAlpha['F' - 'A'];
            }
        }
        break;

        case TUBE_ID_3:
        {
            ledDisplay.data4 = 0;
            ledDisplay.data5 = 0;
            if(isOn)
            {
                ledDisplay.data4 |= displayAlpha['O' - 'A'];
                if(value == TEMPERATURE_UNIT_CELSIUS)
                    ledDisplay.data5 |= displayAlpha['C' - 'A'];
                else
                    ledDisplay.data5 |= displayAlpha['F' - 'A'];
            }
        }
        break;

        default:
            break;
    }
}

void display_tubex(u8 tubex, u8 valueType, u8 value, u8 isPositive, u8 isOn)
{
    switch(valueType)
    {
        case TYPE_VALUE_DEC:
            display_tubex_dec(tubex, value, isPositive, isOn);
        break;

        case TYPE_VALUE_HEX:
            display_tubex_hex(tubex, value, isOn);
        break;

        case TYPE_VALUE_SWITCH:
            display_tubex_switch(tubex, value, isOn);
        break;

        case TYPE_VALUE_UNIT:
            display_tubex_unit(tubex, value, isOn);
        break;

        default:
            break;
    }
}

static void display_advancedMenu(u8 valueType, u8 value)
{
    display_tubex(TUBE_ID_1, valueType, value, 1, 1);
}

static void display_advancedMenuValue(u8 valueType, u8 value, u8 isPositive, u8 isFlicker)
{
    static u8 __xdata preSet;
    if(isFlicker)
    {
        preSet ^= 0x01;
        if(isFlicker == 0x11)
        {
            sysParams.isDisplayFlicker = 0x10;
            preSet = 0x01;
        }
        display_tubex(TUBE_ID_3, valueType, value, isPositive, preSet);
    }
    else
    {
        display_tubex(TUBE_ID_3, valueType, value, isPositive, 1);
    }
}

static void display_temp_floor(u8 value)
{
    display_tubex(TUBE_ID_1, TYPE_VALUE_DEC, value, 1, 1);
    if(sysParams.regs.temperatureUnit == TEMPERATURE_UNIT_FAHRENHEIT)
        display_ledx(1, 8, 1);
    else
        display_ledx(0, 8, 1);
}

static void display_temp_airConditioner(u8 value)
{
    display_tubex(TUBE_ID_2, TYPE_VALUE_DEC, value, 1, 1);
    if(sysParams.regs.temperatureUnit == TEMPERATURE_UNIT_FAHRENHEIT)
        display_ledx(3, 8, 1);
    else
        display_ledx(2, 8, 1);
}

static void display_temp_smp(u8 value)
{
    display_tubex(TUBE_ID_3, TYPE_VALUE_DEC, value, 1, 1);
    if(sysParams.regs.temperatureUnit == TEMPERATURE_UNIT_FAHRENHEIT)
        display_ledx(4, 8, 1);
    else    
        display_ledx(5, 8, 1);
}

static void display_power_off(void)
{
    // display_icon_show(ICON_STATUS_WIFI, DISABLE);
    // display_icon_show(ICON_STATUS_LOCK, DISABLE);
    display_icon_show(ICON_STATUS_ELECTRIC_VALVE, DISABLE);
    display_icon_show(ICON_STATUS_FLOOR_HEATING, DISABLE);
    display_icon_show(ICON_MODE_ECONOMIC, DISABLE);
    display_icon_show(ICON_WIND_SPEED_AUTO, DISABLE);
    display_icon_show(ICON_WIND_SPEED_LOW, DISABLE);
    display_icon_show(ICON_WIND_SPEED_MEDIUM, DISABLE);
    display_icon_show(ICON_WIND_SPEED_HIGH, DISABLE);
    display_icon_show(ICON_BTN_POWER, ENABLE);
    display_icon_show(ICON_BTN_PAGE, ENABLE);
    display_icon_show(ICON_BTN_OTHER, DISABLE);
    display_tubex(TUBE_ID_1, 0, 0, 0, 0);
    display_tubex(TUBE_ID_2, 0, 0, 0, 0);
    display_advancedMenuValue(TYPE_VALUE_SWITCH, 0, 0, 0);
    /* 无操作 */
    if(sysParams.isNoOperation)
    {
        display_set_luminance(displayLuminanceTab[0]);
        display_icon_show(ICON_BTN_POWER, ENABLE);
        display_icon_show(ICON_BTN_PAGE, ENABLE);
        display_icon_show(ICON_BTN_OTHER, DISABLE);
    }
    else
    {
        display_set_luminance(displayLuminanceTab[sysParams.backligntLevel]);
        display_icon_show(ICON_BTN_POWER, ENABLE);
        display_icon_show(ICON_BTN_PAGE, ENABLE);
        display_icon_show(ICON_BTN_OTHER, ENABLE);
    }
}

static void display_page_airConditioner(void)
{
    /* 空调模式常亮 */
    switch(sysParams.regs.airConditionerMode)
    {
        case AIR_CONDITIONER_MODE_COOLING:
            display_icon_show(ICON_MODE_COOLING, ENABLE);
            display_icon_show(ICON_MODE_HEATING, DISABLE);
            display_icon_show(ICON_MODE_AIR_CIRCULATION, DISABLE);
            display_icon_show(ICON_MODE_DEHUMIDIFICATION, DISABLE);
            break;
        case AIR_CONDITIONER_MODE_HEATING:
            display_icon_show(ICON_MODE_COOLING, DISABLE);
            display_icon_show(ICON_MODE_HEATING, ENABLE);
            display_icon_show(ICON_MODE_AIR_CIRCULATION, DISABLE);
            display_icon_show(ICON_MODE_DEHUMIDIFICATION, DISABLE);
            break;
        case AIR_CONDITIONER_MODE_AIR_CIRCULATION:
            display_icon_show(ICON_MODE_COOLING, DISABLE);
            display_icon_show(ICON_MODE_HEATING, DISABLE);
            display_icon_show(ICON_MODE_AIR_CIRCULATION, ENABLE);
            display_icon_show(ICON_MODE_DEHUMIDIFICATION, DISABLE);
            break;
        case AIR_CONDITIONER_MODE_DEHUMIDIFICATION:
            display_icon_show(ICON_MODE_COOLING, DISABLE);
            display_icon_show(ICON_MODE_HEATING, DISABLE);
            display_icon_show(ICON_MODE_AIR_CIRCULATION, DISABLE);
            display_icon_show(ICON_MODE_DEHUMIDIFICATION, ENABLE);
            break;
        default:
            break;
    }
    /* 地暖指示灯熄灭 */
    display_icon_show(ICON_MODE_FLOOR_HEATING, DISABLE);
    /* 新风指示灯熄灭 */
    display_icon_show(ICON_MODE_FRESH_AIR, DISABLE);
    /* 地暖设置温度数码管熄灭 */
    display_tubex(TUBE_ID_1, 0, 0, 0, 0);
    /* 地暖阀输出状态指示灯熄灭 */
    display_icon_show(ICON_STATUS_FLOOR_HEATING, DISABLE);
    /* 空调处于开机状态 */
    if(sysParams.regs.powerStatus & POWER_AIR_CONDITIONER_MASK)    
    {
        /* 无操作 */
        if(sysParams.isNoOperation)
        {
            display_set_luminance(displayLuminanceTab[1]);
            display_icon_show(ICON_BTN_POWER, ENABLE);
            display_icon_show(ICON_BTN_PAGE, DISABLE);
            display_icon_show(ICON_BTN_OTHER, DISABLE);
            // PrintString1("No operation\n\r");
        }
        else
        {
            display_set_luminance(displayLuminanceTab[sysParams.backligntLevel]);
            display_icon_show(ICON_BTN_POWER, ENABLE);
            display_icon_show(ICON_BTN_PAGE, ENABLE);
            display_icon_show(ICON_BTN_OTHER, ENABLE);
        }
        /* 风速设置 */
        if(sysParams.regs.airConditionerMode != AIR_CONDITIONER_MODE_DEHUMIDIFICATION)
        {
            switch(sysParams.regs.airConditionerSpeedSet)
            {
                case SET_WIND_SPEED_AUTO:
                    display_icon_show(ICON_WIND_SPEED_AUTO, ENABLE);
                    switch(sysParams.regs.airConditionerSpeedOut)
                    {
                        case OUTPUT_WIND_OFF:
                            display_icon_show(ICON_WIND_SPEED_HIGH, DISABLE);
                            break;
                        case OUTPUT_WIND_SPEED_LOW:
                            display_icon_show(ICON_WIND_SPEED_LOW, ENABLE);
                            break;
                        case OUTPUT_WIND_SPEED_MEDIUM:
                            display_icon_show(ICON_WIND_SPEED_MEDIUM, ENABLE);
                            break;
                        case OUTPUT_WIND_SPEED_HIGH:
                            display_icon_show(ICON_WIND_SPEED_HIGH, ENABLE);
                            break;
                    }
                    break;
                case SET_WIND_SPEED_LOW:
                    display_icon_show(ICON_WIND_SPEED_AUTO, DISABLE);
                    display_icon_show(ICON_WIND_SPEED_LOW, ENABLE);
                    break;
                case SET_WIND_SPEED_MEDIUM:
                    display_icon_show(ICON_WIND_SPEED_AUTO, DISABLE);
                    display_icon_show(ICON_WIND_SPEED_MEDIUM, ENABLE);
                    break;
                case SET_WIND_SPEED_HIGH:
                    display_icon_show(ICON_WIND_SPEED_AUTO, DISABLE);
                    display_icon_show(ICON_WIND_SPEED_HIGH, ENABLE);
                    break;
            }
        }
        else
        {
            display_icon_show(ICON_WIND_SPEED_HIGH, DISABLE);
        }
        
        /* 室内温度 */
        display_temp_smp(sysParams.regs.temperatureSmp);
        /* 空调设定温度 */
        switch(sysParams.regs.airConditionerMode)
        {
            case AIR_CONDITIONER_MODE_AIR_CIRCULATION:
            case AIR_CONDITIONER_MODE_DEHUMIDIFICATION:
                display_tubex(TUBE_ID_2, 0, 0, 0, 0);
            break;
            case AIR_CONDITIONER_MODE_COOLING:
            case AIR_CONDITIONER_MODE_HEATING:
                display_temp_airConditioner(sysParams.regs.temperatureAirSet);
            break;
        }
        /* 输出状态 */
        (sysParams.regs.outputStatus & OUTPUT_STATUS_AIR_CONDITIONER_MASK) ? display_icon_show(ICON_STATUS_ELECTRIC_VALVE, ENABLE) : display_icon_show(ICON_STATUS_ELECTRIC_VALVE, DISABLE);
    }
    else    //空调处于关机状态
    {
        display_power_off();
    }
}

static void display_page_floorHeating(void)
{
    /* 地暖指示灯常亮 */
    display_icon_show(ICON_MODE_FLOOR_HEATING, ENABLE);
    /* 空调指示灯熄灭 */
    display_icon_show(ICON_MODE_COOLING, DISABLE);
    display_icon_show(ICON_MODE_HEATING, DISABLE);
    display_icon_show(ICON_MODE_AIR_CIRCULATION, DISABLE);
    display_icon_show(ICON_MODE_DEHUMIDIFICATION, DISABLE);
    /* 新风指示灯熄灭 */
    display_icon_show(ICON_MODE_FRESH_AIR, DISABLE);
    /* 空调设置温度数码管熄灭 */
    display_tubex(TUBE_ID_2, 0, 0, 0, 0);
    /* 空调阀输出状态指示灯熄灭 */
    display_icon_show(ICON_STATUS_ELECTRIC_VALVE, DISABLE);
    /* 风速相关指示灯熄灭 */
    display_icon_show(ICON_WIND_SPEED_HIGH, DISABLE);
    display_icon_show(ICON_WIND_SPEED_AUTO, DISABLE);
    /* 地暖开机 */
    if(sysParams.regs.powerStatus & POWER_FLOOR_HEATING_MASK)    
    {
        /* 无操作 */
        if(sysParams.isNoOperation)
        {
            display_set_luminance(displayLuminanceTab[1]);
            display_icon_show(ICON_BTN_POWER, ENABLE);
            display_icon_show(ICON_BTN_PAGE, DISABLE);
            display_icon_show(ICON_BTN_OTHER, DISABLE);
            // PrintString1("No operation\n\r");
        }
        else
        {
            display_set_luminance(displayLuminanceTab[sysParams.backligntLevel]);
            display_icon_show(ICON_BTN_POWER, ENABLE);
            display_icon_show(ICON_BTN_PAGE, ENABLE);
            display_icon_show(ICON_BTN_OTHER, ENABLE);
        }
        /* 室内温度 */
        display_temp_smp(sysParams.regs.temperatureSmp);
        /* 地暖设定温度 */
        display_temp_floor(sysParams.regs.temperatureFloorSet);
        /* 输出状态 */
        (sysParams.regs.outputStatus & OUTPUT_STATUS_FLOOR_HEATING_MASK) ? display_icon_show(ICON_STATUS_FLOOR_HEATING, ENABLE) : display_icon_show(ICON_STATUS_FLOOR_HEATING, DISABLE);
    }
    else
    {
        display_power_off();
    }
}

static void display_page_freshAir(void)
{
    /* 新风指示灯常亮 */
    display_icon_show(ICON_MODE_FRESH_AIR, ENABLE);
    /* 地暖指示灯熄灭 */
    display_icon_show(ICON_MODE_FLOOR_HEATING, DISABLE);
    /* 空调指示灯熄灭 */
    display_icon_show(ICON_MODE_COOLING, DISABLE);
    display_icon_show(ICON_MODE_HEATING, DISABLE);
    display_icon_show(ICON_MODE_AIR_CIRCULATION, DISABLE);
    display_icon_show(ICON_MODE_DEHUMIDIFICATION, DISABLE);
    /* 空调阀输出状态指示灯熄灭 */
    display_icon_show(ICON_STATUS_ELECTRIC_VALVE, DISABLE);
    /* 地暖阀输出状态指示灯熄灭 */
    display_icon_show(ICON_STATUS_FLOOR_HEATING, DISABLE);
    /* 空调设置温度数码管熄灭 */
    display_tubex(TUBE_ID_2, 0, 0, 0, 0);
    /* 地暖设置温度数码管熄灭 */
    display_tubex(TUBE_ID_1, 0, 0, 0, 0);
    /* 新风开机 */
    if(sysParams.regs.powerStatus & POWER_FRESH_AIR_MASK)    
    {
        /* 无操作 */
        if(sysParams.isNoOperation)
        {
            display_set_luminance(displayLuminanceTab[1]);
            display_icon_show(ICON_BTN_POWER, ENABLE);
            display_icon_show(ICON_BTN_PAGE, ENABLE);
            display_icon_show(ICON_BTN_OTHER, DISABLE);
        }
        else
        {
            display_set_luminance(displayLuminanceTab[sysParams.backligntLevel]);
            display_icon_show(ICON_BTN_POWER, ENABLE);
            display_icon_show(ICON_BTN_PAGE, ENABLE);
            display_icon_show(ICON_BTN_OTHER, ENABLE);
        }
        /* 风速设置 */
        switch(sysParams.regs.freshAirSpeedSet)
        {
            case SET_WIND_SPEED_LOW:
                display_icon_show(ICON_WIND_SPEED_AUTO, DISABLE);
                display_icon_show(ICON_WIND_SPEED_LOW, ENABLE);
                break;
            case SET_WIND_SPEED_MEDIUM:
                display_icon_show(ICON_WIND_SPEED_AUTO, DISABLE);
                display_icon_show(ICON_WIND_SPEED_MEDIUM, ENABLE);
                break;
            case SET_WIND_SPEED_HIGH:
                display_icon_show(ICON_WIND_SPEED_AUTO, DISABLE);
                display_icon_show(ICON_WIND_SPEED_HIGH, ENABLE);
                break;
        }
        /* 室内温度 */
        display_temp_smp(sysParams.regs.temperatureSmp);
    }
    else
    {
        display_power_off();
    }
}

static void display_page_engineer(void)
{
    display_icon_clear(); 
    display_set_luminance(displayLuminanceTab[sysParams.backligntLevel]);
    display_advancedMenu(TYPE_VALUE_DEC, sysParams.advancedMenu);
    switch(sysParams.advancedMenu)
    {
        case ENGINEER_MENU_TEMPERATURE_CALIBRATION:
            if(sysParams.regs.temperatureCalibration >= 0)
                display_advancedMenuValue(TYPE_VALUE_DEC, sysParams.regs.temperatureCalibration, 1, sysParams.isDisplayFlicker);
            else
                display_advancedMenuValue(TYPE_VALUE_DEC, -sysParams.regs.temperatureCalibration, 0, sysParams.isDisplayFlicker);
        break;
        case ENGINEER_MENU_MODE_ECONOMIC:
            display_advancedMenuValue(TYPE_VALUE_SWITCH, sysParams.regs.isEconomicMode, 1, sysParams.isDisplayFlicker);
        break;
        case ENGINEER_MENU_AIR_BLOWER_CONTROLLED:
            display_advancedMenuValue(TYPE_VALUE_SWITCH, sysParams.regs.isAirBlowerControlled, 1, sysParams.isDisplayFlicker);
        break;
        case ENGINEER_MENU_LOW_TEMPERATURE_PROTECTED:
            display_advancedMenuValue(TYPE_VALUE_SWITCH, sysParams.regs.isLowTemperatureProtected, 1, sysParams.isDisplayFlicker);  
        break;
        case ENGINEER_MENU_POWER_OFF_MEMORY:
            display_advancedMenuValue(TYPE_VALUE_SWITCH, sysParams.regs.isPowerOffMemory, 1, sysParams.isDisplayFlicker);  
        break;
        case ENGINEER_MENU_BACKLASH_UPPER_LIMIT:
            if(sysParams.regs.backlashUpperLimit >= 0)
                display_advancedMenuValue(TYPE_VALUE_DEC, sysParams.regs.backlashUpperLimit, 1, sysParams.isDisplayFlicker);
            else
                display_advancedMenuValue(TYPE_VALUE_DEC, -sysParams.regs.backlashUpperLimit, 0, sysParams.isDisplayFlicker);
        break;
        case ENGINEER_MENU_BACKLASH_LOWER_LIMIT:
            if(sysParams.regs.backlashLowerLimit >= 0)
                display_advancedMenuValue(TYPE_VALUE_DEC, sysParams.regs.backlashLowerLimit, 1, sysParams.isDisplayFlicker);
            else
                display_advancedMenuValue(TYPE_VALUE_DEC, -sysParams.regs.backlashLowerLimit, 0, sysParams.isDisplayFlicker);
        break;
        case ENGINEER_MENU_TEMPERATURE_UPPER_LIMIT:
            if(sysParams.regs.temperatureUpperLimit >= 0)
                display_advancedMenuValue(TYPE_VALUE_DEC, sysParams.regs.temperatureUpperLimit, 1, sysParams.isDisplayFlicker);
            else
                display_advancedMenuValue(TYPE_VALUE_DEC, -sysParams.regs.temperatureUpperLimit, 0, sysParams.isDisplayFlicker);
        break;
        case ENGINEER_MENU_TEMPERATURE_LOWER_LIMIT:
            if(sysParams.regs.temperatureLowerLimit >= 0)
                display_advancedMenuValue(TYPE_VALUE_DEC, sysParams.regs.temperatureLowerLimit, 1, sysParams.isDisplayFlicker);
            else
                display_advancedMenuValue(TYPE_VALUE_DEC, -sysParams.regs.temperatureLowerLimit, 0, sysParams.isDisplayFlicker);
        break;
        case ENGINEER_MENU_COOLING_SUPPORTED:
            display_advancedMenuValue(TYPE_VALUE_SWITCH, sysParams.regs.airConditionerModeSupport & AIR_CONDITIONER_SUPPORT_COOLING_MASK, 1, sysParams.isDisplayFlicker);
        break;
        case ENGINEER_MENU_HEATING_SUPPORTED:
            display_advancedMenuValue(TYPE_VALUE_SWITCH, sysParams.regs.airConditionerModeSupport & AIR_CONDITIONER_SUPPORT_HEATING_MASK, 1, sysParams.isDisplayFlicker);
        break;
        case ENGINEER_MENU_AIR_CIRCULATION_SUPPORTED:
            display_advancedMenuValue(TYPE_VALUE_SWITCH, sysParams.regs.airConditionerModeSupport & AIR_CONDITIONER_SUPPORT_AIR_CIRCULATION_MASK, 1, sysParams.isDisplayFlicker);
        break;
        case ENGINEER_MENU_DEHUMIDIFICATION_SUPPORTED:
            display_advancedMenuValue(TYPE_VALUE_SWITCH, sysParams.regs.airConditionerModeSupport & AIR_CONDITIONER_SUPPORT_DEHUMIDIFICATION_MASK, 1, sysParams.isDisplayFlicker);
        break;
        case ENGINEER_MENU_TEMPERATURE_UNIT:
            display_advancedMenuValue(TYPE_VALUE_UNIT, sysParams.regs.temperatureUnit, 1, sysParams.isDisplayFlicker);
        break;
        case ENGINEER_MENU_COMMUNICATION_ADDR:
            display_advancedMenuValue(TYPE_VALUE_HEX, sysParams.commAddr, 1, sysParams.isDisplayFlicker);
        break;

        default:
        break;
    }
}

static void display_page_advanced(void)
{
    display_icon_clear(); 
    display_set_luminance(displayLuminanceTab[sysParams.backligntLevel]);
    display_advancedMenu(TYPE_VALUE_DEC, sysParams.advancedMenu);
    switch(sysParams.advancedMenu)
    {
        case ADVANCED_MENU_VERSION_INFO:
            display_advancedMenuValue(TYPE_VALUE_HEX, SOFTWARE_VERSION, 1, 0);
        break;
        case ADVANCED_MENU_BRADRATE:
            display_advancedMenuValue(TYPE_VALUE_DEC, sysParams.commBradrate, 1, sysParams.isDisplayFlicker);
        break;
        case ADVANCED_MENU_MACHINE_SUPPORTED:
            display_advancedMenuValue(TYPE_VALUE_DEC, sysParams.regs.machineTypeSupported, 1, sysParams.isDisplayFlicker);
        break;
        case ADVANCED_MENU_LUMINANCE:
            display_advancedMenuValue(TYPE_VALUE_DEC, sysParams.backligntLevel, 1, sysParams.isDisplayFlicker);
        break;
        case ADVANCED_MENU_RELAY_MODE:
            display_advancedMenuValue(TYPE_VALUE_DEC, sysParams.relayMode, 1, sysParams.isDisplayFlicker);
        break;
        default:
        break;
    }
}

static void display_page_error(void)
{
    display_clear();
    if(sysParams.Fault == FAULT_TYPE_SENSOR)
        display_tubex(TUBE_ID_3, TYPE_VALUE_HEX, 0xE1, 0, 1);
}

/* 显示处理函数 */
void display_func(void)
{   
    /* 按键锁 */
    sysParams.regs.isBtnLocked ? display_icon_show(ICON_STATUS_LOCK, ENABLE) : display_icon_show(ICON_STATUS_LOCK, DISABLE);
    /* 入网状态(暂不支持) */
    
    /* 分界面显示 */
    switch(sysParams.presentPage)
    {
        /* 空调界面 */
        case PAGE_AIR_CONDITIONER:
        {
            display_page_airConditioner();
        }
        break;

        /* 地暖界面 */
        case PAGE_FLOOR_HEATING:
        {
            display_page_floorHeating();
        }
        break;

        /* 新风界面 */
        case PAGE_FRESH_AIR:
        {
            display_page_freshAir();
        }
        break;

        /* 工程师界面 */
        case PAGE_ENGINEER:
        {
            display_page_engineer();
        }
        break;

        /* 高级界面 */
        case PAGE_ADVANCED:
        {
            display_page_advanced();
        }
        break;

        /* 错误界面 */
        case PAGE_ERROR:
        {
            display_page_error();
        }
        break;
        
        default:
        break;

    }

    led_display();
}

void display_init(void)
{
    tm1640_init();
    gpio_config();
    display_set_luminance(displayLuminanceTab[1]);
}


// void display_test(void)
// {

//     led_display();
// }
